package cn.life.common.support.check;

import cn.life.business.user.dao.UserDao;
import cn.life.common.entity.model.CMessage;
import cn.life.common.entity.model.MyUser;
import cn.life.common.entity.model.MyCommon;
import cn.life.common.entity.model.MyUserInfo;
import cn.life.common.support.exception.CException;
import cn.life.common.utils.EncryptPwdUtil;
import cn.life.common.utils.JwtUtil;
import cn.life.business.user.dao.impl.UserDaoImpl;
import cn.life.business.user.entity.po.UserInfo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.util.WebUtils;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description:
 *
 * @Author 李佳明
 * @Date 2018/7/17 0017 下午 3:30
 */
public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {

    /** 登录用户信息session中的key */
    private String sessionKey = MyCommon.MY_SESSION_KEY;

    /** 登录用户信息的class类型 */
    private Class<?> sessionMemberClass = MyUserInfo.class;

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        // 过滤出符合条件的参数，这里指的是加了MemberDetail注解的参数
        if (parameter.hasParameterAnnotation(MyUser.class)) {
            return true;
        }
        return false;
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);

        Object val = null;

        Cookie[] cookies = request.getCookies();
        if(cookies != null && cookies.length > 0){
            for(Cookie cookie : cookies){
                if(cookie.getName().equals("factoryCookie")){
                    sessionKey = cookie.getValue();
                }
            }
        }

        // 获取session中存放的用户信息
        Object sessionMember = WebUtils.getSessionAttribute(request, sessionKey);
        if(sessionMember != null){
            return sessionMember;
        }

        //获取token信息是否存在
        String token = request.getHeader("token");
        Claims claims = JwtUtil.verifyToken(token);
        if(claims != null){
            String user = claims.getSubject();
            String[] userData = user.split(",");
            MyUserInfo userInfo = new MyUserInfo();
            userInfo.setUserId(userData[0]);
            userInfo.setUserName(userData[1]);
            val = userInfo;
            return val;
        }

        // session中对象与加了MemberDetail注解的参数的类型相同是赋值给这个参数
        Class<?> klass = parameter.getParameterType();
        if (klass.isAssignableFrom(sessionMemberClass)){
            if(sessionMember == null){
                CException cException = new CException("1001","参数错误");
                cException.printStackTrace();
            }else{
                return sessionMember;
            }
        }

        return null;
    }
}
